package com.example.demo.service.impl;

import com.example.demo.dto.CouponMonitorDTO;
import com.example.demo.mapper.CouponMonitorMapper;
import com.example.demo.mapper.entity.StudentCouponsView;
import com.example.demo.service.CouponMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class CouponMonitorServiceImpl implements CouponMonitorService {
    @Autowired
    private CouponMonitorMapper mapper;
    DecimalFormat df = new DecimalFormat("#.##");
    @Override
    public List<CouponMonitorDTO> getCouponsData(int userId, int totalNum) {
        //1为代金券 2为满减劵 3为折扣劵 4为兑换券
        List<CouponMonitorDTO> couponsDataList = new ArrayList<>();
        CouponMonitorDTO voucherData = setCouponsData(1, userId, totalNum);
        voucherData.setName("代金券");
        voucherData.setCouponsType(1);
        couponsDataList.add(voucherData);
        // 2 满减劵
        CouponMonitorDTO fullDiscountData = setCouponsData(2, userId, totalNum);
        fullDiscountData.setName("满减劵");
        fullDiscountData.setCouponsType(2);
        couponsDataList.add(fullDiscountData);
        // 3 折扣劵
        CouponMonitorDTO discountData = setCouponsData(3, userId, totalNum);
        discountData.setName("折扣劵");
        discountData.setCouponsType(3);
        couponsDataList.add(discountData);
        // 4 兑换券
        CouponMonitorDTO exchangeData = setCouponsData(4, userId, totalNum);
        exchangeData.setName("兑换券");
        exchangeData.setCouponsType(4);
        couponsDataList.add(exchangeData);

        return couponsDataList;
    }

    @Override
    public List<StudentCouponsView> getCouponsDateData() {
        List<StudentCouponsView> studentCouponsList = mapper.getCouponsView();
        return studentCouponsList;
    }

    @Override
    public CouponMonitorDTO getCouponsReportData(int userId,int totalNum) {
        CouponMonitorDTO couponsData = new CouponMonitorDTO();
        if(userId!=0){
            int totalCouponsNum = mapper.getCouponsTotalCount(userId);
            int usedCouponsNum = mapper.getUsedCouponsNum(userId);
            couponsData.setTotalCouponsNum(totalCouponsNum);
            couponsData.setUsedCouponsNum(usedCouponsNum);
            //使用率 = (使用的优惠券数量 / 发放的优惠券总数) * 100%
            double usageRate=Double.valueOf(df.format((double) usedCouponsNum/ totalCouponsNum * 100));
            couponsData.setUsageRate(usageRate);
            //优惠券的顾客平均销售额比未使用优惠券的顾客高出【销售额提升百分比】%
            /*销售额提升百分比 = ((使用优惠券的顾客平均销售额 - 未使用优惠券的顾客平均销售额) / 未使用优惠券的顾客平均销售额) * 100%*/
            int unusedCouponsNum = totalNum - usedCouponsNum;
            double salesIncrease=Double.valueOf(df.format((double) (usedCouponsNum-unusedCouponsNum)/unusedCouponsNum * 100));
            couponsData.setSalesIncrease(salesIncrease);
            int mostUsedCouponsType = mapper.getMostUsedCoupons(userId);
            //1为代金券 2为满减劵 3为折扣劵 4为兑换券
            if(mostUsedCouponsType==1) couponsData.setMostUsedTypeName("代金券");
            if(mostUsedCouponsType==2) couponsData.setMostUsedTypeName("满减劵");
            if(mostUsedCouponsType==3) couponsData.setMostUsedTypeName("折扣劵");
            if(mostUsedCouponsType==4) couponsData.setMostUsedTypeName("兑换券");

            int leastUsedCouponsType =  mapper.getLeastUsedCoupons(userId);
            if(leastUsedCouponsType==1) couponsData.setLeastUsedTypeName("代金券");
            if(leastUsedCouponsType==2) couponsData.setLeastUsedTypeName("满减劵");
            if(leastUsedCouponsType==3) couponsData.setLeastUsedTypeName("折扣劵");
            if(leastUsedCouponsType==4) couponsData.setLeastUsedTypeName("兑换券");

            //优惠券类型得分 = (领先优惠券数量 - 落后优惠券数量) / 总优惠券数量 * 最大类型得分系数
            double couponsScore = (double) (mostUsedCouponsType - leastUsedCouponsType) / (totalNum * 50);
            // 综合评分 = 0.3 * 使用率得分 + 0.25 * 转化率得分 + 0.20 * 销售额提升百分比得分 + 0.25 * 优惠券类型得分
            // 假设 usageRate、salesIncrease 和 couponsScore 都是 double 类型的变量
            double result = 0.55 * usageRate + 0.2 * salesIncrease + 0.25 * couponsScore;
            // 使用 Math.round() 对结果进行四舍五入并转换为整数
            result = Math.round(result);
            // 结果需要限制在 [0, 100] 范围内，因此使用 Math.min() 和 Math.max() 函数
            result = Math.min(Math.max(result, 0), 100);
           // 最后，将结果赋值给一个整数变量（如果需要）
            couponsData.setTotalScore((int) result);
        }
        return couponsData;
    }

    @Override
    public CouponMonitorDTO getStatisticData(int userId) {
        CouponMonitorDTO couponsData = new CouponMonitorDTO();
        if(userId!=0){
            int voucherNum = mapper.getVoucherNum(userId);
            int fullDiscountNum = mapper.getFullDiscountNum(userId);
            int discountNum = mapper.getDiscountNum(userId);
            int exchangeNum = mapper.getExchangeNum(userId);
            couponsData.setVoucherNum(voucherNum);
            couponsData.setFullDiscountNum(fullDiscountNum);
            couponsData.setDiscountNum(discountNum);
            couponsData.setExchangeNum(exchangeNum);
            return couponsData;
        }
        return null;
    }

    private CouponMonitorDTO setCouponsData(int type, int userId, int totalNum) {
        CouponMonitorDTO couponsData = new CouponMonitorDTO();

        // 假设权重分别为 30%、30% 和 40%
        double weightUsage = 0.3;
        double weightConversion = 0.3;
        double weightSalesIncrease = 0.4;

        System.out.println("优惠券种类:"+type);
        //已使用优惠券数量
        int usedCoupons = mapper.getUsedCoupons(userId,type);
        System.out.println("usedCoupons返回值"+usedCoupons);
        //发放的优惠券总数
        int couponsCount = mapper.getCouponsCount(userId,type);
        System.out.println("couponsCount返回值"+couponsCount);
        //优惠券使用率:使用率 = (使用的优惠券数量 / 发放的优惠券总数) * 100%
        double usageRate = (double)usedCoupons / couponsCount * 100;
       // usageRate = Double.valueOf(df.format(usageRate));
        couponsData.setUsageRate(Double.valueOf(df.format(usageRate)));
        //优惠券转化率:转化率 = (使用优惠券的订单数 / 发放的优惠券总数) * 100%
        double percentConversion = (double)usedCoupons / couponsCount * 100;
        couponsData.setPercentConversion(Double.valueOf(df.format(percentConversion)));
        //计算销售提升:销售提升 = (使用优惠券的总销售额 / 同期未使用优惠券的总销售额) * 100% - 100%
        double salesIncrease = (double)usedCoupons / (totalNum-usedCoupons) * 100 - 100;
        // String formattedSalesIncrease = String.format("%.2f", salesIncrease);
        couponsData.setSalesIncrease(Double.valueOf(df.format(salesIncrease)));
        // 计算加权平均得分
        double weightedAverage = (usageRate * weightUsage) + (percentConversion * weightConversion) + (salesIncrease * weightSalesIncrease);
        couponsData.setWeightedAverage(weightedAverage); // 将计算结果存储到CouponMonitorDTO对象中的一个新属性中

        return couponsData;
    }
}
